<include file="Public/header"/>
<body class="body-bg">
<div class="park-header">
    <div class="park-header__title">
        我的订单
    </div>
    <div class="park-header__left" onclick="window.location='/Home/User/index/';">
        <i class="iconfont icon-fanhui"></i>
    </div>
</div>
<div class="park-order-nav-tabs">
    <ul>
        <li <if condition="$type_id eq 1">class="active"</if> onclick="window.location='/Home/User/order/type_id/1/';">全部</li>
        <li <if condition="$type_id eq 2">class="active"</if> onclick="window.location='/Home/User/order/type_id/2/';">待付款</li>
        <li <if condition="$type_id eq 3">class="active"</if> onclick="window.location='/Home/User/order/type_id/3/';">待发货</li>
        <li <if condition="$type_id eq 4">class="active"</if> onclick="window.location='/Home/User/order/type_id/4/';">待收货</li>
        <li <if condition="$type_id eq 5">class="active"</if> onclick="window.location='/Home/User/order/type_id/5/';">待评价</li>
    </ul>
</div>
<div class="container">
    <div class="park-cart-store-list" id="orderlist"></div>
</div>
<script src="__ROOT__/Public/Home/layer/layer.js"></script>
<script>
    var options = {
        type: 'get', // default 'get'
        dataType: 'html' // default 'json'
    };
    $(function () {
        var url = '/Home/User/order/';
        var type_id = '{$type_id}';
        var params = {'act':'ajax','type_id':type_id};
        PARK.dropLoad(PARK.dropAjax(options, url, params, function (data) {
            var data = JSON.parse(data);
            var html = '';
            for (var i = 0; i < data.result.length; i++) {
                if(data['result'][i]['status']==1){
                    var status_name = '<span class="park-green">待支付</span>';
                }else if(data['result'][i]['status']==2 && data['result'][i]['order_status']<1){
                    var status_name = '<span class="park-orange">待发货</span>';
                }else if(data['result'][i]['status']==2 && data['result'][i]['order_status']==1){
                    var status_name = '<span class="park-orange">待收货</span>';
                }else if(data['result'][i]['status']==4){
                    var status_name = '<span class="park-red">已取消</span>';
                }else if(data['result'][i]['status']==2 && data['result'][i]['order_status']==2 && data['result'][i]['is_comment']<1){
                    var status_name = '<span class="park-green">待评价</span>';
                }else if(data['result'][i]['status']==2 && data['result'][i]['order_status']==2 && data['result'][i]['is_comment']==1){
                    var status_name = '<span class="park-green">已完成</span>';
                }
                html+='<div class="park-cart-store-item order{$val.id}">';
                html+='        <div class="park-cart-store-item__title">';
                html+='        <div class="left"><span>订单号：'+data['result'][i]['order_sn']+'</span></div>';
                html+='<div class="right">'+status_name+'</div>';
                html+='</div>';
                html+='<div class="park__goods">';
                html+='        <ul>';
                for(var j=0;j<data['result'][i]['goods_list'].length;j++){
                    html+='        <li onclick="goods_detail('+data['result'][i]['goods_list'][j]['goods_id']+','+data['result'][i]['goods_list'][j]['store_id']+');">';
                    html+='        <div class="park-goods__pic" style="background-image: url('+data['result'][i]['goods_list'][j]['thumb']+')"></div>';
                    html+='        <div class="park-goods__info">';
                    html+='        <p class="name">'+data['result'][i]['goods_list'][j]['goods_name']+'</p>';
                    html+='<p class="desc">'+data['result'][i]['goods_list'][j]['store_name']+'</p>';
                    html+='<p class="spec">规格：'+data['result'][i]['goods_list'][j]['spec_name']+'</p>';
                    html+='<p class="price-line">';
                    html+='    <span class="price">￥<em>'+data['result'][i]['goods_list'][j]['left_price']+'</em>'+data['result'][i]['goods_list'][j]['right_price']+'</span>';
                    html+='    </p>';
                    html+='    </div>';
                    html+='    <div class="park-goods__pos">';
                    html+='   <span class="num">x'+data['result'][i]['goods_list'][j]['goods_number']+'</span>';
                    html+='</div>';
                    html+='</li>';
                }
                html+='</ul>';
                html+='</div>';
                html+='<div class="park-order-edit">';
                if(data['result'][i]['status']==1) {
                    html += '        <div class="park-order-edit__btn park-order-edit__btn--orange" onclick="sub_imme(' + data['result'][i]['id'] + ');">立即支付</div>';
                    html += '        <div class="park-order-edit__btn park-order-edit__btn--orange" onclick="sub_cancel(' + data['result'][i]['id'] + ');">取消订单</div>';
                }else if(data['result'][i]['status']==2 && data['result'][i]['order_status']==2 && data['result'][i]['is_comment']<1){
                    html += '       <div class="park-order-edit__btn park-order-edit__btn--green" onclick="order_detail(' + data['result'][i]['id'] + ');">查看详情</div>';
                    html += '       <div class="park-order-edit__btn park-order-edit__btn--green" onclick="add_comment(' + data['result'][i]['id'] + ');">添加评论</div>';
                }else if(data['result'][i]['status']==2 && data['result'][i]['order_status']==1){
                    html += '       <div class="park-order-edit__btn park-order-edit__btn--green" onclick="order_detail(' + data['result'][i]['id'] + ');">查看详情</div>';
                    html += '       <div class="park-order-edit__btn park-order-edit__btn--green" onclick="confirm_receipt(' + data['result'][i]['id'] + ');">确认收货</div>';
                }else {
                    html += '       <div class="park-order-edit__btn park-order-edit__btn--green" onclick="order_detail(' + data['result'][i]['id'] + ');">查看详情</div>';
                }
                html+='</div>';
                html+='</div>';
            }
            if(html) {
                $('#orderlist').append(html);
            }else{
                $('#orderlist').append('<div style="text-align:center;color:#ccc;margin-top:100px;">暂无订单</div>');
            }
            if(data.is_page==1){
                PARK.unDropLoad();
            }
        }));
        $(document).on('click', '.park-order-nav-tabs li', function () {
            var li = $('.park-order-nav-tabs li');
            li.removeClass('active');
            $(this).addClass('active');
        });
		pushHistory(); 
	    window.addEventListener("popstate", function(e) { 
		 	window.location = '/Home/User/order/';
	    }, false); 
    });
	function pushHistory() { 
		var state = { 
			title: "title", 
			url: "#"
		}; 
		window.history.pushState(state, "title", "#"); 
	}	
    function goods_detail(id,store_id){
        window.location = "{:U('Home/Index/detail',"",false)}/id/"+id+'/store_id/'+store_id;
    }
    function order_detail(id){
        window.location = '/Home/User/order_detail/order_id/'+id;
    }
    function sub_cancel(id){
        layer.open({
            content: '您确定要取消该订单吗？'
            ,btn: ['确定','取消']
            ,yes: function(index){
                $.post('/Home/User/del_order/',{order_id:id},function(data){
                    if(data.code==200){
                        window.location = '/Home/User/order/';
                    }
                },'json');
            }
        });
    }
    //添加评论
    function add_comment(id){
        window.location = '/Home/User/add_comment/order_id/'+id;
    }
    //确认收货
    function confirm_receipt(id){
        layer.open({
            content: '您确定收到商品了吗？'
            ,btn: ['确定','取消']
            ,yes: function(index){
                $.post('/Home/User/confirm_receipt/',{order_id:id},function(data){
                    layer.open({
                        content:data.msg
                        ,btn: ['确定']
                        ,yes: function(index){
                            if(data.code==200){
                                window.location = '/Home/User/order/';
                            }
                            layer.closeAll();
                        }
                    });
                },'json');
            }
        });
    }
	function sub_imme(id){
        window.location = '/Home/Cart/immediately/order_id/'+id;
    }
</script>
</body>
</html>